/*
Kiwi Editor is a framework for building WYSIWYG web editors.
Copyright (C) 2011  Roosevelt de Lima Sardinha

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Contact the author at roosevelt1@gmail.com (Roosevelt) 
*/

$(document).ready(function(){

	wm = new workspaceManager();
    ws = wm.createWorkspace();
	dm = new editorDocumentManager();

    myToolBox = ws.createToolBox('toolBox', {
		left:0,
		top: 30,
		width: 105,
		height: 450,
		parent: 'main'	
	});	
	
    myPropertyBox = ws.createPropertyBox('propertyBox', {
		left:0,
		top: 490,
		width: 890,
		height: 200,
		parent: 'main'
	});
		
    myInstructionBox = ws.createInstructionBox('instructionBox', {
		left:780, 
		top:30, 
		width:110, 
		height:450, 
		parent:'main'
	});
	
    myEditionArea = ws.createEditionArea('editionArea', {
        left: 115,
        top: 30,
        width: 655,
        height: 455,
        parent: 'main'
    });
	   
    doc = dm.createDocument();
	doc.open(myEditionArea);
	
    myToolBox.addTool("textboxEO", "Textbox", {
        hint: "Textbox",
        instructionBox: 'instructionBox',
		propertyBox: 'propertyBox',
        help: 'Creates a textbox, a box where you can insert some text. Textboxes are usually used in forms.'
    });
	
    myToolBox.addTool("formTextbox", "Form Textbox", {
        instructionBox: 'instructionBox',
		propertyBox: 'propertyBox',
		help: 'Creates a textbox with a label.'
    });
	
	myToolBox.addTool("radioEO", "Radio", {
        hint: "Radio",
        instructionBox: 'instructionBox',
		propertyBox: 'propertyBox',
        help: 'Creates a radio button. It is similar to the checkbox, but you can choose only one option in a set of ones.'
    });
    
    myToolBox.addTool("checkboxEO", "Checkbox", {
        hint: "Checkbox",
        instructionBox: 'instructionBox',
		propertyBox: 'propertyBox',
        help: 'Creates a checkbox, you can check it or uncheck it as you wish. It has also a label, describing the option you are checking.'
    });

    myToolBox.addTool("label", "Label", {
        hint: "Label",
        instructionBox: 'instructionBox',
		propertyBox: 'propertyBox',
        help: 'Creates a label. A small piece of text.'
    });
	
	myToolBox.addTool("textEO", "Text", {
        hint: "Text",
        instructionBox: 'instructionBox',
		propertyBox: 'propertyBox',
        help: 'Adds some formatted text to the document.'
    });  
	
	myToolBox.addTool("image", "Image", {
        hint: "Image",
        instructionBox: 'instructionBox',
		propertyBox: 'propertyBox',
        help: 'Adds a custom image to the document.'
    }); 
	
    var rm = new roomenus('kiwi-menu', {
        width: 895
    });

    rm.appendTo('main');
    rm.add('Workspace', 0);
	rm.menus[0].add('Load Workspace1', 0);
    rm.menus[0].menus[0].setAction(function(){
		ws.removeEditionArea('editionArea');
		ws.removeToolBox('toolBox');
		ws.removePropertyBox('propertyBox');
		ws.removeInstructionBox('instructionBox');
		ws = wm.loadWorkspace(6);
    });
	    
    rm.menus[0].add('Load Workspace 2', 1);
    rm.menus[0].menus[1].setAction(function(){
		ws.removeEditionArea('editionArea');
		ws.removeToolBox('toolBox');
		ws.removePropertyBox('propertyBox');
		ws.removeInstructionBox('instructionBox');
        ws = wm.loadWorkspace(8);
    });

    rm.add('Document', 1);
    
    rm.menus[1].add('Open', 0);
    rm.menus[1].menus[0].setAction(function(){
        doc = dm.get(11);
		doc.open(ws.getEditionAreaById('editionArea'));
    });

    rm.menus[1].add('Save', 1);
    rm.menus[1].menus[1].setAction(function(){
        doc.name = 'teste';
		doc.description = 'teste';
		doc.save();
    });
	
	rm.menus[1].add('Export HTML', 2);
    rm.menus[1].menus[2].setAction(function(){
        alert(doc.generateHTML());
    });
	
	rm.menus[1].add('Export JSON', 3);
    rm.menus[1].menus[3].setAction(function(){
        alert(doc.generateJSON());
    });
	
	rm.menus[1].add('Export XML', 4);
    rm.menus[1].menus[4].setAction(function(){
		doc.name = 'teste';
		doc.description ='teste';
		doc.save();
        alert(doc.generateXML());
    });

    rm.add('Profile', 2);
	rm.menus[2].add('Load Profile 1', 0);
    rm.menus[2].menus[0].setAction(function(){
        ws.getToolBoxById("toolBox").removeAllTools();
		ws.getToolBoxById("toolBox").loadProfile(6, {});
    });
	
	rm.menus[2].add('Load Profile 2', 1);
    rm.menus[2].menus[1].setAction(function(){
		ws.getToolBoxById("toolBox").removeAllTools();
        ws.getToolBoxById("toolBox").loadProfile(7, {});
    });
	
	rm.add('Edition Area', 3);
			
	rm.menus[3].add('Resize', 0);
    rm.menus[3].menus[0].setAction(function(){
        ws.getEditionAreaById('editionArea').setProperty('width',200);
		ws.getEditionAreaById('editionArea').setProperty('height',200);
    });
	
	rm.menus[3].add('Change Stylesheet', 1);
    rm.menus[3].menus[1].setAction(function(){
        ws.getEditionAreaById('editionArea').setCSS("demo/blueEditionArea.css");
    });

	rm.menus[3].add('Change SnapTo', 2);
    rm.menus[3].menus[2].setAction(function(){
        ws.getEditionAreaById('editionArea').setSnapGrid(50, 50);
    });    
	
	rm.add('Toolbox', 4);
	rm.menus[4].add('Add a Tool', 0);
    rm.menus[4].menus[0].setAction(function(){
		myToolBox.addTool("date", "Date", {
	        hint: "Date",
	        instructionBox: 'instructionBox',
			propertyBox: 'propertyBox',
	        help: 'A field to select a date.'
    	}); 
	});    
	
	rm.add('EO', 5);
	rm.menus[5].add('Append Textbox', 0);
    rm.menus[5].menus[0].setAction(function(){
		myEditionArea = ws.getEditionAreaById('editionArea');
		var textbox = myEditionArea.EOM.createEO('textboxEO');
		myEditionArea.appendObject(textbox);
		textbox.setPropertyBox('propertyBox');
		var red = true;
		var t = setInterval(function(){
				textbox.setLeft(textbox.getLeft() + 20);
				if (red) {
					textbox.setProperty('bgcolor', '#0000FF');
					red = false;
				}
				else {
					textbox.setProperty('bgcolor', '#FF0000');
					red = true;
				}
				textbox.onRemove = function(){clearInterval(t)}; 					
		}, 1000);			 
    });    
 


})
